﻿using System.Security.Claims;
using System.Text;
using LocalTest.Application.Contracts.Shared;
using LocalTest.Web.Consts;

namespace LocalTest.Web.Controllers;

using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;

[Route("[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
    private IConfiguration Configuration { get; }

    public AuthController(IConfiguration configuration)
    {
        this.Configuration = configuration;
    }

    [AllowAnonymous]
    [HttpPost("login")]
    public IActionResult Login()
    {
        // Request.HttpContext.SignInAsync();
        List<Claim> claims =
        [
            new(SystemToken.UIdToken, Guid.NewGuid().ToString()),
        ];
        var token = new JwtSecurityToken(
            issuer: Configuration["Jwt:Issuer"],
            audience: Configuration["Jwt:Audience"],
            claims: claims,
            notBefore: DateTime.Now,
            expires: DateTime.Now.AddDays(2),
            signingCredentials: new SigningCredentials(
                new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"])),
                SecurityAlgorithms.HmacSha256Signature
            )
        );
        JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
        return new JsonResult(new ResponseResult() { Data = handler.WriteToken(token) });
    }
}